<template>
  <el-dialog 
    title="配置版本历史" 
    :visible.sync="dialogVisible"
    width="700px"
    append-to-body
  >
    <el-timeline>
      <el-timeline-item
        v-for="(version, index) in history"
        :key="index"
        :timestamp="formatTime(version.timestamp)"
        placement="top"
      >
        <el-card>
          <div class="version-header">
            <h4>{{ version.description }}</h4>
            <el-button 
              type="text" 
              size="mini" 
              @click="$emit('restore', index)"
            >恢复此版本</el-button>
          </div>
          <div class="version-info">
            <span>参数组: {{ getVersionGroupCount(version) }}</span>
            <span>参数: {{ getVersionParamCount(version) }}</span>
          </div>
        </el-card>
      </el-timeline-item>
    </el-timeline>
    <div slot="footer">
      <el-button @click="dialogVisible = false">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'HistoryDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    history: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit('update:visible', value);
      }
    }
  },
  methods: {
    formatTime(timestamp) {
      return new Date(timestamp).toLocaleString();
    },
    getVersionGroupCount(version) {
      return version.config.paramGroups ? version.config.paramGroups.length : 0;
    },
    getVersionParamCount(version) {
      let count = 0;
      if (version.config.paramGroups) {
        version.config.paramGroups.forEach(group => {
          if (group.isNested && group.childGroups) {
            group.childGroups.forEach(childGroup => {
              count += childGroup.paramItems ? childGroup.paramItems.length : 0;
            });
          } else {
            count += group.paramItems ? group.paramItems.length : 0;
          }
        });
      }
      return count;
    }
  }
};
</script>

<style scoped>
.version-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.version-header h4 {
  margin: 0;
  font-size: 14px;
}
.version-info {
  font-size: 12px;
  color: #909399;
}
.version-info span {
  margin-right: 15px;
}
</style>